<?php
namespace Home\Controller;

use Think\Controller;


class SearchController extends CommonController
{
  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }
  public function shangp()
  {
    $name = trim(I("name"));
    $page = I("page", 1);
    $page_size = I("page_size", 10);

    $where = "products p on p.shop_id=s.id";
      if ($name) {
      $where .= " And p.name like '%$name%'";
    }

    $list = M("shops")->alias('s')
            ->field("s.id as sid,s.name as sname,s.descrip as sdescrip,s.logo as slogo")
            ->where("s.status=1 And s.id!='' And p.status=1 And s.app_id=$this->app_id")
            ->join("right join ".C("DB_PREFIX").$where)->page($page,$page_size)->group('sid')->select();

     foreach($list as $k=>$v){
     $wh = "shop_id=".$v['sid']." And status=1";
      if ($name) {
        $wh .= " And name like '%$name%'";
      }
      $list[$k]['product'] = M("products")->where($wh)->select();
       
     }
    if (empty($list)){
      return_data("error", "index.users.empty");
    } else {
      return_data("success", "index.users.success", $list);
    }
  }

  public function dianp()
  {
    $name = trim(I("name"));
    $page = I("page", 1);
    $page_size = I("page_size", 10);

    $where = "status = 1 And app_id=$this->app_id";
    if ($name) {
      $where .= " And name like '%$name%'";
    }

    $list = M("shops")->where($where)->field("*")->order('id')->page($page.",".$page_size)->select();

    if (empty($list)) {
      return_data("error", "index.users.empty");
    } else {
      return_data("success", "index.users.success", $list);
    }
  }

}
